<template>
  <div class="app-container">
    <el-card>
      <el-form
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          v-show="showSearch"
      >
        <el-form-item label="申请编号">
          <el-input
              v-model="queryParams.applyCode"
              placeholder="请输入申请编号"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="老人姓名">
          <el-input
              v-model="queryParams.applyName"
              placeholder="请输入老人姓名"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="申请状态">
          <el-select
              v-model="queryParams.applyStatus"
              clearable
              placeholder="请选择状态"
              style="width: 240px"
          >
            <el-option
                v-for="dict in apply_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="addDeviceTypes"
          >新增</el-button
          >
        </el-col>

        <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="postList" stripe border max-height="500">
        <el-table-column label="申请编号" align="center" prop="applyCode"  width="200" />
        <el-table-column label="老人姓名" align="center" prop="applyName" width="100" />
        <el-table-column label="性别" align="center" width="100" >
          <template #default="scope">
        <span>
           {{sys_user_sex.find(item=>item.value==scope.row.sex)? sys_user_sex.find(item=>item.value==scope.row.sex).label:''}}

        </span>
          </template>
        </el-table-column>
        <el-table-column label="年龄" align="center" prop="age"   width="100" />

        <el-table-column label="申请日期" align="center" prop="applyDate"  width="150" />

        <el-table-column label="家庭住址" align="center">
          <template #default="scope">
            <spna>{{scope.row.livingAddrProvinceName}}{{scope.row.livingAddrCityName}}{{scope.row.livingAddrCountyName}}{{scope.row.livingAddrStreetName}}{{scope.row.livingAddrCommunityName}}{{scope.row.livingDetailAddress}}
            </spna>
          </template>
        </el-table-column>
        <el-table-column label="申请状态" align="center"  width="120" >
          <template #default="scope">
            <span>
              {{apply_status.find(item=>item.value==scope.row.applyStatus)? apply_status.find(item=>item.value==scope.row.applyStatus).label:''}}
            </span>
          </template>
        </el-table-column>
        <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width" width="220">
          <template #default="scope">
            <el-button link type="primary"  @click="edit(scope.row)" v-if="scope.row.applyStatus=='0'||scope.row.applyStatus=='3'">修改</el-button>
            <el-button link type="primary"  @click="info(scope.row)">查看</el-button>
            <el-button
                v-if="scope.row.applyStatus=='0'||scope.row.applyStatus=='3'"
                link
                type="danger"
                @click="handleDelete(scope.row)"
            >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.page"
          v-model:limit="queryParams.limit"
          @pagination="getList"
      />
    </el-card>
    <el-dialog
        v-model="open"
        :title="title"
        append-to-body
        width="900px"
        v-if="open"
        @close="cancel"
        :close-on-click-modal="false"
    >

     <addEditHomebed @closed="cancel" :formData="formData" :showBtn="true"></addEditHomebed>
    </el-dialog>

    <el-dialog
        v-model="open1"
        title="查看床位申请"
        append-to-body
        width="900px"
        v-if="open1"
        @close="cancel1"
        :close-on-click-modal="false"
    >

      <home-bed-info :formData="formData" @closed="close1()"></home-bed-info>
    </el-dialog>
    <!-- 添加或修改岗位对话框 -->
<!--    <info></info>-->

  </div>
</template>

<script setup name="apply">

const { proxy } = getCurrentInstance();
const { sys_normal_disable, sys_user_sex,apply_status} = proxy.useDict(
    "sys_normal_disable",
    "sys_user_sex",
    "apply_status",

);
import {
  homeBedApply,homeBedItemDel,homeBedApplyInfo
} from "@/api/homebedsign/index";
import addEditHomebed from "./add-edit-homebed.vue"
 import  homeBedInfo  from './info.vue'

const showSearch = ref(true);
const loading = ref(true);
const postList = ref([]);
const open = ref(false);
const open1 = ref(false);
const title = ref("新增床位申请");

const total = ref(0);

const data = reactive({
  formData: {},
  queryParams: {
    page: 1,
    limit: 10,
    applyStatus: undefined,
    applyName:undefined,
    applyCode:undefined
  },
  rules: {

  },
});

const { queryParams, formData, rules } = toRefs(data);

/** 提交按钮 */

function cancel(data) {
  if(data){
    getList();
  }
  open.value = false;
  formData.value={}

}
function edit(data) {
  homeBedApplyInfo(data.id).then(res=>{
    console.log(res)
    formData.value=res.homeBedApply
    open.value=true
    title.value='编辑床位申请'
  })
}
function close1(){
  open1.value=false
  formData.value={}
}

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  homeBedApply(queryParams.value).then((response) => {
    postList.value = response.page.list;
    total.value = response.page.totalCount;
    loading.value = false;
  });
}
function handleQuery() {
  getList();
}
function resetQuery() {
  queryParams.value = {
    page: 1,
    limit: 10,
    applyStatus: undefined,
    applyName:undefined,
    applyCode:undefined
  };
  getList();
}
 function info(data){
   homeBedApplyInfo(data.id).then(res=>{
     console.log(res)
     formData.value=res.homeBedApply
     open1.value=true
   })
 }
 function cancel1(){
   open1.value=false
 }
function handleDelete(row) {
  proxy.$modal
      .confirm("是否确认删除该数据项？")
      .then(function () {
        return homeBedItemDel([row.id]);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      })
      .catch(() => {});
}
function addDeviceTypes() {
  title.value = "新增床位申请";
  open.value = true;
}

getList();
</script>
